package com.fang.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client extends Thread {
    private Socket socket;
    public Client() {
        super("客户端：");
    }

    @Override
    public void run() {

            try {
                socket = new Socket("127.0.0.1", 5543);
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                while (true) {
                    /////////////////////////////发送数据////////////////////////////////
                    Scanner sc = new Scanner(System.in);
                    String strMsg = sc.next();
                    System.out.println(this.getName() + strMsg);
                    bw.write(Thread.currentThread().getName() + strMsg);
                    Thread.sleep(1000);
                    bw.newLine();
                    bw.flush();

                    ////////////////////////////////接收数据////////////////////////////////////////
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String strSou = br.readLine();
                    System.out.println(strSou);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }

    }
}
